package cn.dhn_app.horseman_app.service.impl;

import cn.dhn_app.horseman_app.entity.*;
import cn.dhn_app.horseman_app.mapper.horseman_orderItemMapper;
import cn.dhn_app.horseman_app.mapper.horseman_orderMapper;
import cn.dhn_app.horseman_app.server.feign.FeignInterface;
import cn.dhn_app.horseman_app.service.HorsemanOrderService;
import cn.itcast.util.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author lyy
 * @Description TODO
 * @PACKAGE_NAME: cn.dhn_app.horseman_app.service.impl
 * @ClassName HorsemanOrderServiceImpl
 * @Date 2020/8/27 17:10
 * @DAY_NAME_FULL: 星期四
 * @Version 1.0
 **/
@Service
public class HorsemanOrderServiceImpl implements HorsemanOrderService {

    @Resource
    private FeignInterface feignTokenInterface;

    @Resource
    private horseman_orderMapper horseman_orderMapper;

    @Resource
    private horseman_orderItemMapper horseman_orderItemMapper;

    /**
     * 功能描述: <br>
     * 〈根据骑手openId查询各状态订单〉〉
     * @Author: lyy
     * @Date: 2020/8/27 17:13
     * @Param: [token, openId, status]
     * @Return: cn.itcast.util.ResponseEntity
     */
    @Override
    public ResponseEntity FindAllOrderAndStatus(String token, String openId, String status) {

        if(token.isEmpty()) return new ResponseEntity(401,"token参数为空！！！");
        token tokenEntity =this.feignTokenInterface.getToken(token);
        if(tokenEntity==null) return new ResponseEntity(402,"登陆超时,请重新登陆！！！");
        if(openId.isEmpty()) return new ResponseEntity(401,"openId参数为空！！！");

        horseman_orderExample example = new horseman_orderExample();
        horseman_orderExample.Criteria criteria = example.createCriteria();
        criteria.andHorsemanAccountEqualTo(openId);

        //判断订单状态
        if(status!=null ){
            criteria.andOrderStateEqualTo(status);
        }

        List<horseman_order> orderList =horseman_orderMapper.selectByExample(example);

        for (int i = 0 ; i <orderList.size() ;i++){
            horseman_orderItemExample oIEx = new horseman_orderItemExample();
            horseman_orderItemExample.Criteria criteria1 = oIEx.createCriteria();
            criteria1.andOrderIdEqualTo(orderList.get(i).getId());
            List<horseman_orderItem> orderItems = horseman_orderItemMapper.selectByExample(oIEx);
            orderList.get(i).setOrderItems(orderItems);
        }

        if(!orderList.isEmpty() && orderList.size()!=0){
            return new ResponseEntity(200,"骑手订单数据！！！",orderList);
        }else{
            return new ResponseEntity(403,"骑手没有接到订单！！！");
        }
    }


    /**
     * 功能描述: <br>
     * 〈修改订单状态〉
     * @Author: lyy
     * @Date: 2020/10/14 17:06
     * @Param: [token, openId, orderId, status]
     * @Return: cn.itcast.util.ResponseEntity
     */
    @Override
    public ResponseEntity updHorsemanOrderAndStatus(String token, String openId, Integer orderId, String status) {
        if(token.isEmpty()) return new ResponseEntity(401,"token参数为空！！！");
        token tokenEntity =this.feignTokenInterface.getToken(token);
        if(tokenEntity==null) return new ResponseEntity(402,"登陆超时,请重新登陆！！！");
        if(openId.isEmpty()) return new ResponseEntity(401,"openId参数为空！！！");
        if(orderId.equals("")) return new ResponseEntity(401,"orderId参数为空！！！");
        if(status.isEmpty()) return new ResponseEntity(401,"status参数为空！！！");

        //根据订单id查询订单信息
        horseman_order order = horseman_orderMapper.selectByPrimaryKey(orderId);

        order.setHorsemanAccount(openId);   //修改骑手openId

        order.setOrderState(status);        //修改订单状态

        Integer mdf = horseman_orderMapper.updateByPrimaryKey(order);

        if(mdf == 1){
            return new ResponseEntity(200,"修改成功");
        }else{
            return new ResponseEntity(403,"修改失败");
        }

    }
}
